package com.server.http.handler;

import org.apache.mina.core.future.IoFutureListener;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.server.conditionenum.HttpReturnStatus;
import com.server.http.handler.domain.JsonUtil;
import com.server.http.handler.domain.ReturnEntity;
import com.server.http.request.HttpRequestMessage;
import com.server.http.response.HttpResponseMessage;
import com.server.mine.net.HttpProtocol;

public class HttpServerHandler extends IoHandlerAdapter  {

	static final Logger logger = LoggerFactory.getLogger(HttpServerHandler.class);

	static final String HTTP_BASE_LOGIC_PACKAGE = "com.keyking.coin.service.http.handler.impl";
	
    @Override  
    public void sessionOpened(IoSession session) {  
        // set idle time to 60 seconds  
        session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
    }  
  
    @SuppressWarnings("unchecked")
	@Override  
    public void messageReceived(IoSession session, Object message) { 
        HttpRequestMessage request = (HttpRequestMessage) message;
        int key = -1;
        try {
        	key = Integer.parseInt(request.getParameter("logic"));
        }catch(Exception e) {
        	//e.printStackTrace();
        	logger.info("Logic error," + request.getParameter("logic") + " is a nonexistent logic!");
        }
        
        String logicName = null;
        if(key != -1 && HttpProtocol.httpMap.containsKey(key)) {
        	 logicName = HttpProtocol.httpMap.get(key);
        }
        Class<? extends HttpHandler> clazz = null;
        HttpResponseMessage response = new HttpResponseMessage();
		try {
			
//			if (logicName.contains(".js")){
//				HttpJs js = new HttpJs(logicName);
//				response.appendBody(js.handle(request,response)); 
//			}else{
//				clazz = (Class<? extends HttpHandler>)Class.forName("com.keyking.http.handler.impl." + logicName);
//				HttpHandler handler = clazz.newInstance();
//				handler.handle(request,response); 
//			}
			//if("".equals(logicName) || logicName == null){
			if("".equals(logicName) || logicName == null){
				ReturnEntity ret = ReturnEntity.HttpReturn(null,-1,HttpReturnStatus.LogicNonentity);
				response.appendBody(JsonUtil.ObjectToJsonString(ret));
			}else{
				logger.info("logicName : " + logicName);
				clazz = (Class<? extends HttpHandler>)Class.forName("com.server.http.handler.impl." + logicName);
				HttpHandler handler = clazz.newInstance();
				handler.handle(request,response); 
			}
			
		}catch(Exception e){
//			if (!(e instanceof ClassNotFoundException)){
			e.printStackTrace();
			logger.info("HttpServerHandler:http handler error"+e);
//			}
		}
		response.setResponseCode(HttpResponseMessage.HTTP_STATUS_SUCCESS);
//        response.appendBody("uid="+uid+",name="+name);
		session.write(response).addListener(IoFutureListener.CLOSE);
    }  
  
    @Override  
    public void sessionIdle(IoSession session, IdleStatus status) {  
        session.close(true);
    }  
  
    @Override  
    public void exceptionCaught(IoSession session, Throwable cause) {  
    	cause.printStackTrace();
        session.close(true);
    }  
}
